package com.mf.web.authority;

import com.mf.common.sso.LoginContext;
import com.mf.domain.authority.ResourceInfo;
import com.mf.domain.general.PageBean;
import com.mf.service.authority.ResourceInfoService;
import com.mf.web.framework.advice.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源信息Controller
 * User: zhaoming
 * DateTime: 2016-07-14
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/web/resourceInfo")
public class ResourceInfoController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ResourceInfoService resourceInfoService;

    /**
     * 初始化页面
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("authority/resource/resource_info_index");
        return modelAndView;
    }

    /**
     * 新增页面
     * @param resourceInfo 资源信息Domain
     * @return 模型视图
     */
    @RequestMapping(value = "/insertPage", method = RequestMethod.GET)
    public ModelAndView insertPage(ResourceInfo resourceInfo) {
        ModelAndView modelAndView = new ModelAndView("authority/resource/resource_info_add");
        if(resourceInfo !=null && resourceInfo.getId() != null){
            resourceInfo = resourceInfoService.selectByPrimaryKey(resourceInfo);
        }else{
            resourceInfo.setResourceCode("ROOT");
        }
        modelAndView.addObject("resourceInfo", resourceInfo);
        return modelAndView;
    }

    /**
     * 新增资源信息
     * @param resourceInfo 资源信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean insert(ResourceInfo resourceInfo) {
        boolean flag = resourceInfoService.insert(resourceInfo, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("新增失败");
        }
        return flag;
    }

    /**
     * 维护页面
     * @param resourceInfo 资源信息Domain
     * @return 模型视图
     */
    @RequestMapping(value = "/updatePage", method = RequestMethod.POST)
    public ModelAndView updatePage(ResourceInfo resourceInfo) {
        ModelAndView modelAndView = new ModelAndView("authority/resource/resource_info_edit");
        modelAndView.addObject("resourceInfo", resourceInfoService.selectByPrimaryKey(resourceInfo));
        return modelAndView;
    }

    /**
     * 根据主键维护资源信息
     * @param resourceInfo 资源信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean updateByPrimaryKey(ResourceInfo resourceInfo) {
        boolean flag = resourceInfoService.updateByPrimaryKey(resourceInfo, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("维护失败");
        }
        return flag;
    }

    /**
     * 根据主键删除资源信息 (物理删除)
     * @param resourceInfo 资源信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKey(ResourceInfo resourceInfo){
        boolean flag = resourceInfoService.deleteByPrimaryKey(resourceInfo);
        if(!flag){
            throw new BusinessException("删除资源信息失败");
        }
        return flag;
    }

    /**
     * 根据主键删除资源信息 (逻辑删除)
     * @param resourceInfo 资源信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKeyLogically", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKeyLogically(ResourceInfo resourceInfo){
        boolean flag = resourceInfoService.deleteByPrimaryKeyLogically(resourceInfo, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("删除资源信息失败");
        }
        return flag;
    }

    /**
     * 根据主键查询资源信息
     * @param resourceInfo 资源信息Domain
     * @return 资源信息
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ResourceInfo selectByPrimaryKey(ResourceInfo resourceInfo) {
        return resourceInfoService.selectByPrimaryKey(resourceInfo);
    }

    /**
     * 根据条件查询资源信息
     * @param resourceInfo 资源信息Domain
     * @return 资源信息列表
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ResourceInfo> selectByCondition(ResourceInfo resourceInfo) {
        return resourceInfoService.selectByCondition(resourceInfo);
    }

    /**
     * 分页查询资源信息
     * @param resourceInfo 资源信息Domain
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 资源信息列表
     */
    @RequestMapping(value = "/pageQuery", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> pageQuery(ResourceInfo resourceInfo,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(value = "rows", required = false, defaultValue = "10") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        PageBean<ResourceInfo> pageBean = new PageBean<>(page, pageSize);
        LinkedHashMap<String, String> sortItemMap = new LinkedHashMap<>();
        sortItemMap.put(sortName, sortOrder);
        pageBean.setSortItemMap(sortItemMap);
        pageBean = resourceInfoService.pageQuery(pageBean, resourceInfo);

        Map<String, Object> result = new HashMap<>();
        result.put("root", pageBean.getUnderly());
        result.put("page", pageBean.getPage());
        result.put("total", pageBean.getPageCount());
        result.put("records", pageBean.getItemCount());
        return result;
    }

    /**
     * 查询资源信息列表(树形)
     * @param resourceInfo 资源信息
     * @return 资源信息列表
     */
    @RequestMapping(value = "/treeGirdQuery", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> treeGirdQuery(ResourceInfo resourceInfo) {
        List<ResourceInfo> resourceInfos = resourceInfoService.treeGirdQuery(resourceInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", resourceInfos);
        return result;
    }

    /**
     * 检查重复数据
     * @return 验证结果
     */
    @RequestMapping(value = "/checkRepeat", method = {RequestMethod.GET,RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkRepeat(ResourceInfo resourceInfo) {
        return resourceInfoService.checkRepeat(resourceInfo);
    }

}